/**
 * @description: 查找树节点
 * @param origin 数据源
 * @param findValue 找到节点
 * @param key 默认id
 * @return T
 */
export function findTreeNode<T extends TreeData>(origin: T[], findValue: any, key: keyof T = "id"): T | null {
  let result: T | null = null;

  const fun = (data: T[]) => {
    if (Array.isArray(data)) {
      for (let i = 0; i < data.length; i++) {
        let item = data[i];
        if (item[key] === findValue) {
          result = item;
          return;
        }
        if (item.children && item.children.length) {
          fun(item.children as T[]);
        }
      }
    }
  };

  fun(origin);
  return result;
}
